﻿using Gl_mes.DAL.MesDB;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MesGLApp.DAL.MesDB
{
    public class MesDB : DbContext
    {
        public MesDB() : base("name=MesDB")
        {
            this.Database.Log = LogToConsole;
        }

        private void LogToConsole(string message)
        {
            Console.WriteLine(message);
        }

        /// <summary>
        /// 站点信息
        /// </summary>
        public virtual DbSet<SiteInfo> SiteInfos { get; set; }

        /// <summary>
        /// 站点组别
        /// </summary>
        public virtual DbSet<SiteGroup> SiteGroups { get; set; }

        /// <summary>
        /// 站点与组别的内联关系
        /// </summary>
        public virtual DbSet<SiteInfoGroup> SiteInfoGroups { get; set; }

        /// <summary>
        /// 原始工单
        /// </summary>
        public virtual DbSet<SourceProdProcess> SourceProdProcesses { get; set; }

        /// <summary>
        /// 拆分后的工单
        /// </summary>
        public virtual DbSet<TargetProdProcess> TargetProdProcesses { get; set; }

        /// <summary>
        /// 工单与站点组绑定关系
        /// </summary>
        public virtual DbSet<ProdProcSite> ProdProcSites { get; set; }

        /// <summary>
        /// 工单的扫描记录
        /// </summary>
        public virtual DbSet<ProdProcScan> ProdProcScans { get; set; }

        /// <summary>
        /// 状态类别 
        /// </summary>
        public virtual DbSet<StatusType> StatusTypes { get; set; }

        /// <summary>
        /// 状态值
        /// </summary>
        public virtual DbSet<StatusInfo> StatusInfos { get; set; }

        /// <summary>
        /// 修改日志
        /// </summary>
        public virtual DbSet<MesLog> MesLogs { get; set; }


        /// <summary>
        /// 产品生产周期设定
        /// </summary>
        public virtual DbSet<ProdCycle> ProdCycle { get; set; }


        /// <summary>
        /// 原始工单生产周期设定
        /// </summary>
        public virtual DbSet<SourcePPCycle> SourcePPCycle { get; set; }

        /// <summary>
        /// 实际工单的生产周期
        /// </summary>
        public virtual DbSet<TargetPPCycle> TargetPPCycle { get; set; }

        /// <summary>
        /// 异常订单
        /// </summary>
        public virtual DbSet<ExcepOrder> ExcepOrder { get; set; }

        /// <summary>
        /// 用户站点分配
        /// </summary>
        public virtual DbSet<UserSite> UserSite { get; set; }


        public virtual DbSet<ExcepPicture> ExcepPictures { get; set; }           
        
        /// <summary>
        /// 生产线
        /// </summary>
        public virtual DbSet<ProdLineSetting> ProdLineSettings { get; set; }


        /// <summary>
        /// 排单计划
        /// </summary>
        public virtual DbSet<ProdSchedule> ProdSchedules { get; set; }

        /// <summary>
        /// 员工信息
        /// </summary>
        public virtual DbSet<EmplInfo> EmplInfos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SourceProdProcess>()
                .Property(e => e.OrderQty)
                .HasPrecision(18, 5);

            modelBuilder.Entity<SourceProdProcess>()
                .Property(e => e.GoodsLength)
                .HasPrecision(18, 5);

            modelBuilder.Entity<TargetProdProcess>()
                .Property(e => e.OrderQty)
                .HasPrecision(18, 5);

            modelBuilder.Entity<TargetProdProcess>()
                .Property(e => e.GoodsLength)
                .HasPrecision(18, 5);

            modelBuilder.Entity<TargetProdProcess>()
                .Property(e => e.ProdQty)
                .HasPrecision(18, 5);

            modelBuilder.Entity<SourcePPCycle>()
                .Property(e => e.OrderQty)
                .HasPrecision(18, 5);

            modelBuilder.Entity<ProdProcScan>()
                .Property(e => e.ScanQty)
                .HasPrecision(18, 5);
        }
    }
}
